package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjWorkLeaveMessageAnnexMapper;
import com.zb.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单留言附件表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-04-24
 */
@Service
public class ZjWorkLeaveMessageAnnexServiceImpl extends ServiceImpl<ZjWorkLeaveMessageAnnexMapper, ZjWorkLeaveMessageAnnex> implements ZjWorkLeaveMessageAnnexService {
    @Resource
    private ZjWorkLeaveMessageAnnexMapper workLeaveMessageAnnexMapper;
    @Resource
    private ZjWorkLeaveMessageAnnex02Service workLeaveMessageAnnex02Service;
    @Resource
    private ZjWorkLeaveMessageAnnex03Service workLeaveMessageAnnex03Service;
    @Resource
    private ZjWorkLeaveMessageAnnex04Service workLeaveMessageAnnex04Service;
    @Resource
    private ZjWorkLeaveMessageAnnex05Service workLeaveMessageAnnex05Service;
    /**
     * 分表数量
     */
    private final int  TABLE_COUNT = 5;

    // 保存工单留言附件
    @Override
    public void save(List<ZjWorkLeaveMessageAnnex> annexList) {
        if (CollectionUtil.isEmpty(annexList)) {
            return;
        }
        List<ZjWorkLeaveMessageAnnex> annexes = new ArrayList<>();
        List<ZjWorkLeaveMessageAnnex02> annexe02s = new ArrayList<>();
        List<ZjWorkLeaveMessageAnnex03> annexe03s = new ArrayList<>();
        List<ZjWorkLeaveMessageAnnex04> annexe04s = new ArrayList<>();
        List<ZjWorkLeaveMessageAnnex05> annexe05s = new ArrayList<>();
        long index = 0;
        for (ZjWorkLeaveMessageAnnex annex : annexList) {
            index = annex.getId() % TABLE_COUNT;
            if (index == 0) {
                annexes.add(annex);
                continue;
            }
            if (index == 1) {
                annexe02s.add(new ZjWorkLeaveMessageAnnex02(annex));
                continue;
            }
            if (index == 2) {
                annexe03s.add(new ZjWorkLeaveMessageAnnex03(annex));
                continue;
            }
            if (index == 3) {
                annexe04s.add(new ZjWorkLeaveMessageAnnex04(annex));
                continue;
            }
            if (index == 4) {
                annexe05s.add(new ZjWorkLeaveMessageAnnex05(annex));
            }
        }
        if (CollectionUtil.isNotEmpty(annexes)) this.saveBatch(annexes);
        if (CollectionUtil.isNotEmpty(annexe02s)) workLeaveMessageAnnex02Service.saveBatch(annexe02s);
        if (CollectionUtil.isNotEmpty(annexe03s)) workLeaveMessageAnnex03Service.saveBatch(annexe03s);
        if (CollectionUtil.isNotEmpty(annexe04s)) workLeaveMessageAnnex04Service.saveBatch(annexe04s);
        if (CollectionUtil.isNotEmpty(annexe05s)) workLeaveMessageAnnex05Service.saveBatch(annexe05s);
    }

    // 根据工单ID获取留言附件
    @Override
    public List<ZjWorkLeaveMessageAnnex> getByWorkId(Long workId) {
        return workLeaveMessageAnnexMapper.getByWorkId(workId);
    }
}
